<template>
  <div class="mod-order">
    <el-form :inline="true" :model="queryOrderDto">
      <el-form-item>
        <el-input v-model="queryOrderDto.orderNumber" placeholder="订单编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryOrderDto.userName" placeholder="买家账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryOrderDto.sourceType" placeholder="订单来源" clearable>
         <el-option v-for="option in sourceTypeList" :key="option.value" :label="option.key" :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryOrderDto.orderStatus" placeholder="订单状态" clearable>
         <el-option v-for="option in orderStatusList" :key="option.value" :label="option.key" :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryOrderDto.deliveryCompany" placeholder="物流公司" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryOrderDto.receiverName" placeholder="收货人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="resetFind()">重置</el-button>
        <el-button type="primary" @click="getDataList()">搜索</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="id" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="orderNumber" header-align="center" align="center" label="订单编号" width="160"></el-table-column>
      <el-table-column prop="userName" header-align="center" align="center" label="买家账号"></el-table-column>
      <el-table-column prop="sourceType" header-align="center" align="center" label="订单来源" width="80">
        <template slot-scope="scope">
          <el-tag v-for="item in sourceTypeList"  v-if="scope.row.sourceType === item.value" size="small" type="success" :key="item.vaule">
             {{ item.key }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="totalPrice" header-align="center" align="center" label="订单总额"></el-table-column>
      <el-table-column prop="payPrice" header-align="center" align="center" label="应付金额"></el-table-column>
      <el-table-column prop="deliveryCompany" header-align="center" align="center" label="物流公司"></el-table-column>
      <el-table-column prop="receiverName" header-align="center" align="center" label="收货人"></el-table-column>
      <el-table-column prop="receiverAddress" header-align="center" align="center" label="收货地址" width="280"></el-table-column>
      <el-table-column prop="orderStatus" header-align="center" align="center" label="订单状态" width="80">
        <template slot-scope="scope">
          <el-tag v-for="item in orderStatusList"  v-if="scope.row.orderStatus === item.value" size="small" type="success" :key="item.vaule">
             {{ item.key }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="下单时间" width="180"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="240" label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="orderInfo(scope.row.id)">详情</el-button>
          <el-button type="warning" size="mini" @click="addOrUpdateHandle(scope.row.id)" v-if="scope.row.orderStatus === 0 || scope.row.orderStatus === 1">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryOrderDto.pageNum"
      :page-sizes="[10, 20, 50, 100]" :page-size="queryOrderDto.pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        queryOrderDto: {
          orderNumber: '',
          userName: '',
          payType: '',
          sourceType: '',
          orderStatus: '',
          deliveryCompany: '',
          deliveryNumber: '',
          billType: '',
          receiverName: '',
          receiverPhone: '',
          confirmStatus: '',
          pageNum: 1,
          pageSize: 10
        },
        dataList: [],
        totalPage: 0,
        sourceTypeList: [],
        orderStatusList: [],
        dataListSelections: [],
        dataListLoading: false
      }
    },
    activated () {
      this.getDataList()
      this.getEnums()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/mall-order/order/info/listOrders'),
          method: 'post',
          data: this.queryOrderDto
        }).then(({data}) => {
          if (data.code === 200) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 详情
      orderInfo (orderId) {
        console.log(orderId)
        this.$router.push({path: '/order-info', query: { orderId: orderId }})
      },
      // 修改
      addOrUpdateHandle (orderId) {
        this.$router.push({path: '/order-add-or-update', query: { orderId: orderId }})
      },
      // 每页数量变化
      sizeChangeHandle (val) {
        this.queryOrderDto.pageSize = val
        this.queryOrderDto.pageNum = 1
        this.getDataList()
      },
      // 跳转指定页码
      currentChangeHandle (val) {
        this.queryOrderDto.pageNum = val
        this.getDataList()
      },
      // 勾选表格数据
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`是否进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/mall-order/order/info/deleteOrder`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 获取全部枚举
      getEnums () {
        this.$http({
          url: this.$http.adornUrl('/mall-user/user/tool/getEnums'),
          method: 'get'
        }).then(({data}) => {
          this.sourceTypeList = data.data.SourceTypeEnum
          this.orderStatusList = data.data.OrderStatusEnum
        })
      },
      // 重置搜索
      resetFind () {
        this.queryOrderDto.orderNumber = ''
        this.queryOrderDto.userName = ''
        this.queryOrderDto.sourceType = ''
        this.queryOrderDto.orderStatus = ''
        this.queryOrderDto.deliveryCompany = ''
        this.queryOrderDto.receiverName = ''
        this.getDataList()
      }
    }
  }
</script>
